package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.domain.db.Admin;
import com.itheima.web.interceptor.AdminHolder;
import com.itheima.web.manager.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.PriorityBlockingQueue;

@RestController
public class AdminController {

    @Autowired
    private AdminManager adminManager;


    //返回验证码。返回json，直接用响应输出就好了，返回值为void
    @GetMapping("/system/users/verification")
    public void verification(@RequestParam String uuid, HttpServletResponse response) throws IOException {
        //调用manager返回验证码对象
        LineCaptcha verification = adminManager.verification(uuid);
        //通过response响应验证码图片
        verification.write(response.getOutputStream());
    }

    //用户登入
    @PostMapping("/system/users/login")
    public ResponseEntity login(@RequestBody Map<String,String> map){
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");

        return adminManager.login(username,password,verificationCode,uuid);
    }

    //基本信息
    @PostMapping("/system/users/profile")
    public ResponseEntity profile(){
        Admin admin = AdminHolder.get();
        return ResponseEntity.ok(admin);
    }

    //用户登出
    @PostMapping("/system/users/logoutv")
    public void logout(@RequestHeader("Authorization") String token ){
        //把token删除
        adminManager.logout(token);
    }


}
